﻿function recordOriginCoordinate(console_panel_selector) {
    var console = $(console_panel_selector);
    var coord = {};
    $.extend(coord, {
        x: parseInt(console.offset().left),
        y: parseInt(console.offset().top)
    });    
    console.data('origin', coord);
};
function translate(origin, point) {
    point.x = point.x + origin.x;
    point.y = point.y + origin.y;
    return point;
};
function drawCoordinate(console_canvas_selector, radius, color,
    zoom_panel_selector, zoomImageSrc) {
    var consolecanvas = $(console_canvas_selector).get(0);
    if (consolecanvas.getContext) {
        var ctx = consolecanvas.getContext('2d');
        var e = window.event;
        var w0 = consolecanvas.width;
        var h0 = consolecanvas.height;
        //var x0 = Math.ceil(e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft);
        //var y0 = Math.ceil(e.clientY + document.body.scrollTop + document.documentElement.scrollTop);
        var x0 = parseInt(e.offsetX);
        var y0 = parseInt(e.offsetY);
        var x1 = x0 > radius ? x0 - radius : 0;
        var x2 = (x0 + radius) > w0 ? w0 : x0 + radius;
        var y1 = y0 > radius ? y0 - radius : 0;
        var y2 = (y0 + radius) > h0 ? h0 : y0 + radius;

        var w1 = x2 - x1;
        var h1 = y2 - y1;

        if ($(consolecanvas).data('xLine') == null) {
            $(consolecanvas).data('xLine', []);
        }
        if ($(consolecanvas).data('yLine') == null) {
            $(consolecanvas).data('yLine', []);
        }
        if ($(consolecanvas).data('textRect') == null) {
            $(consolecanvas).data('textRect', []);
        }
        var xLine = $(consolecanvas).data('xLine');
        var yLine = $(consolecanvas).data('yLine');
        var textRect = $(consolecanvas).data('textRect');

        if (xLine.length > 0) {
            ctx.clearRect(parseInt(xLine[0]), parseInt(xLine[1]), parseInt(xLine[2]), parseInt(xLine[3]));
        }
        if (yLine.length > 0) {
            ctx.clearRect(parseInt(yLine[0]), parseInt(yLine[1]), parseInt(yLine[2]), parseInt(yLine[3]));
        }
        if (textRect.length > 0) {
            ctx.clearRect(parseInt(textRect[0]), parseInt(textRect[1]), parseInt(textRect[2]), parseInt(textRect[3]));
        }

        if (color != null) {
            ctx.fillStyle = color;
        }

        xLine[0] = x1; xLine[1] = y0; xLine[2] = w1; xLine[3] = 1;
        $(consolecanvas).data('xLine', xLine);
        ctx.fillRect(xLine[0], xLine[1], xLine[2], xLine[3]);

        yLine[0] = x0; yLine[1] = y1; yLine[2] = 1; yLine[3] = h1;
        $(consolecanvas).data('yLine', yLine);
        ctx.fillRect(yLine[0], yLine[1], yLine[2], yLine[3]);

        var text = x0 + ',' + y0;
        textRect[0] = x0 + 10; textRect[1] = y0 - 15; textRect[2] = x2 - x0; textRect[3] = 15;
        $(consolecanvas).data('textRect', textRect);
        ctx.font = 'normal 12px arial';
        ctx.textBaseline = 'top';
        ctx.fillText(text, textRect[0], textRect[1]);

        //var zoomcanvas = $(zoom_panel_selector).find('canvas').get(0);
        //if (zoomcanvas.getContext) {
        //    var zctx = zoomcanvas.getContext('2d');
        //    zctx.clearRect(0, 0, 200, 200);
        //    var zoomImage = new Image();
        //    zoomImage.src = zoomImageSrc;
        //    zctx.drawImage(zoomImage, xLine[0], yLine[1], 200, 200, 0, 0, 200, 200);
        //}
        //dodge(zoom_panel_selector);
    }
    window.event.cancelBubble = true;
};

function drawSpecialPoint(console_selector, terminal) {
    $.extend(terminal, {
        X: terminal.X || 0,
        Y: terminal.Y || 0,
        Id: terminal.Id || 'tp_' + terminal.X + '_' + terminal.Y,
        HostName: terminal.HostName || '未知',
        AlertUrl: terminal.AlertUrl || 'javascript:void(0)',
        AlertCount: terminal.AlertCount || 0,
        State: terminal.State || 'OnLine'
    });
    var p = $('<a id="' + terminal.Id + '" href="' + terminal.AlertUrl + '" class="point"></a>');
    $(console_selector).append(p);
    var origin = $(console_selector).data('origin');
    var point = translate(origin, { x: terminal.X, y: terminal.Y });    
    p.data('coord', { x: terminal.X, y: terminal.Y })
        .css({
            left: (parseInt(point.x) - Math.ceil(p.width() / 2)) + 'px',
            top: (parseInt(point.y) - p.height()) + 'px'
        }).attr('title', '主机名称：' + terminal.HostName + '\r\n告警数量：'
                + terminal.AlertCount + '\r\n' + '主机状态：' + terminal.State);
    if (terminal.State == 'OnLine') {
        p.addClass('online');
    } else {
        p.addClass('offline');
    }
};
function redrawPoint(console_selector) {
    $(console_selector).find('.point').each(function () {
        var p = $(this);
        var origin = $(console_selector).data('origin');
        var point = translate(origin, { x: p.data('coord').x, y: p.data('coord').y });
        p.css({
            left: parseInt(point.x) - Math.ceil(p.width() / 2),
            top: parseInt(point.y) - p.height()
        });
    });
};
function cacheRectOfZoomPanel(zoom_panel_selector) {
    var zpRect = $(zoom_panel_selector).offset();
    zpRect.right = zpRect.right || zpRect.left + $(zoom_panel_selector).width();
    zpRect.bottom = zpRect.bottom || zpRect.top + $(zoom_panel_selector).height();
    zpRect.width = zpRect.width || $(zoom_panel_selector).width();
    zpRect.height = zpRect.height || $(zoom_panel_selector).height();
    $(zoom_panel_selector).data('rect', zpRect);
};
function dodge(zoom_panel_selector) {
    var e = window.event;
    //var x0 = Math.ceil(e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft);
    //var y0 = Math.ceil(e.clientY + document.body.scrollTop + document.documentElement.scrollTop);
    var x0 = parseInt(e.offsetX);
    var y0 = parseInt(e.offsetY);
    var zpRect = $(zoom_panel_selector).data('rect');
    if (x0 >= zpRect.left && x0 <= zpRect.right && y0 >= zpRect.top && y0 <= zpRect.bottom) {
        $(zoom_panel_selector).hide();
        var zoomcanvas = $(zoom_panel_selector).find('canvas').get(0);
        if (zoomcanvas.getContext) {
            var zctx = zoomcanvas.getContext('2d');
            zctx.clearRect(zpRect.left, zpRect.top, zpRect.width, zpRect.height);
        }
    } else {
        $(zoom_panel_selector).show();
    }
};